package org.batatasframework.support.database;

/**
 * <pre>
 * 定义一个数据库表所需要的信息，公共的数据；
 * 
 * 用于描述一个数据库表
 * </pre>
 * @author jackson
 *
 */
public class TableInfo {

	private final String schemaName;
	private final String catalogName;
	private final String tableName;
	private final String sourceName;

	private static String SEPARATOR = ".";
	
	public static String qualify(String schema, String catalog, String name) {
		StringBuilder buff = new StringBuilder();
		if ( schema != null ) {
			buff.append(schema).append(SEPARATOR);
		}
		if ( catalog != null ) {
			buff.append(catalog).append(SEPARATOR);
		}
		buff.append(name);
		return buff.toString();
	}
	
	public TableInfo(String name){
		String[] strs = name.split(SEPARATOR);
		if(strs.length == 3){
			this.schemaName = strs[0];
			this.catalogName = strs[1];
			this.sourceName = strs[2];
		}else if(strs.length == 2){
			this.schemaName = "";
			this.catalogName = strs[0];
			this.sourceName = strs[1];
		}else{
			this.schemaName = "";
			this.catalogName = "";
			this.sourceName = strs[0];
		}
		
		this.tableName = name;
	}
	
	public TableInfo(String schemaName, String catalogName, String tableName) {
		this.schemaName = schemaName;
		this.catalogName = catalogName;
		this.sourceName = tableName;
		this.tableName = qualify(schemaName, catalogName, tableName);
	}

	public TableInfo(TableInfo tableInfo, String tableName) {
		this(tableInfo.schemaName, tableInfo.catalogName, tableName);
	}

	public String getSchemaName() {
		return schemaName;
	}

	public String getCatalogName() {
		return catalogName;
	}

	public String getTableName() {
		return tableName;
	}

	public String getSourceName() {
		return sourceName;
	}
	
}
